﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Yu.Cms.Common;
using Yu.Cms.IDAL;

namespace Yu.Cms.SQLServerDAL
{
    public  class Annonce : IAnnonce
    {
        private readonly string TableName = "Y_Annonce";

        #region IAnnonce 成员

        public int Update(Yu.Cms.Model.Annonce model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + TableName + " set ");
            strSql.Append("announce=@announce,");
            strSql.Append("pubTime=@pubTime");      
            SqlParameter[] parameters = {				
					new SqlParameter("@announce", SqlDbType.NVarChar,200),
					new SqlParameter("@pubTime", SqlDbType.SmallDateTime)};         
            parameters[0].Value = model.announce;
            parameters[1].Value = model.pubTime;

            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Yu.Cms.Model.Annonce GetModel()
        {
            QueryParam qp = new QueryParam();            
            int RecordCount;
            Yu.Cms.Model.Annonce fam = new Yu.Cms.Model.Annonce();
            IList<Yu.Cms.Model.Annonce> lst = this.GetList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        #endregion

        /// <summary>
        /// 将记录集转为Annonce实体类 (Y_Annonce)
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <param name="Fileds">字段名列表</param>
        /// <returns>Annonce</returns>
        protected Yu.Cms.Model.Annonce Populate_Annonce(IDataReader dr, Dictionary<string, string> Fileds)
        {
            Yu.Cms.Model.Annonce nc = new Yu.Cms.Model.Annonce();

            if (Fileds.ContainsKey("id") && !Convert.IsDBNull(dr["id"])) nc.id = Convert.ToInt32(dr["id"]); // 自动ID
            if (Fileds.ContainsKey("announce") && !Convert.IsDBNull(dr["announce"])) nc.announce = Convert.ToString(dr["announce"]).Trim(); // 公告内容
            if (Fileds.ContainsKey("pubTime") && !Convert.IsDBNull(dr["pubTime"])) nc.pubTime = Convert.ToDateTime(dr["pubTime"]); // 发布时间
            return nc;
        }

        /// <summary>
        /// 返回Annonce实体类的IList对象 (Y_Annonce)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>Annonce实体类的IList对象(Y_Annonce)</returns>
        protected IList<Yu.Cms.Model.Annonce> GetList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = TableName;
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "id";
            }
            else if (qp.Orderfld != "id")
            {
                qp.Orderfld += ",id";
            }

            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            else
            {
                qp.ReturnFields += ",";
                qp.ReturnFields += qp.Orderfld;
            }
            DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.Annonce> mypd = new DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.Annonce>(this.Populate_Annonce);
            return DbHelperMsSQL.GetRecordListBySql(mypd, qp, out RecordCount);
        }
    }
}
